package com.renting.modules.sys.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.renting.modules.sys.dao.LogDao;
import com.renting.modules.sys.entity.Log;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LogService {

    @Autowired
    LogDao logDao;


    public void save(HttpServletRequest request) {
        Log log = new Log();
//        log.setUser(StringUtils.isEmpty(request.getSession().getAttribute("user").toString()) ? "" : request.getSession().getAttribute("user").toString());
        log.setUser("admin");
        log.setMethod(request.getMethod());
        log.setParams(request.getParameterMap().toString());
        log.setRemoteAddr(request.getRemoteAddr());
        log.setRequestUri(request.getRequestURI());
        log.setId(RandomUtil.simpleUUID());
        log.setCreateTime(new Date());
        logDao.save(log);
    }

    public List<Log> getAll() {
        List<Log> list = new ArrayList<>();
        logDao.findAll().forEach(log -> {
            list.add(log);
        });
        return list;
    }
}
